package com.example.demo.utils;

import com.example.demo.pojo.UtilityBill;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class BillPdfUtil {
    public ByteArrayOutputStream generatePdf(UtilityBill bill) throws DocumentException, IOException {
        Document document = new Document();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, byteArrayOutputStream);
        document.open();

// 设置字体路径
        String fontPath = "E:\\JetBrains\\songti.ttf";

// 设置中文字体
        Font fontTitle = FontFactory.getFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font font = FontFactory.getFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        // 添加标题
        Paragraph title = new Paragraph("水电费账单", fontTitle);
        title.setAlignment(Element.ALIGN_CENTER);
        document.add(title);

        // 添加用户信息
        Paragraph userInfo = new Paragraph("用户姓名: " + bill.getRealname(), font);
        document.add(userInfo);

        // 添加账单日期信息
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Paragraph billDate = new Paragraph("账单日期: " + dateFormat.format(bill.getCreatetime()) + " 至 " + dateFormat.format(bill.getEndtime()), font);
        document.add(billDate);

        // 创建表格
        PdfPTable table = new PdfPTable(4);
        table.setWidthPercentage(100);

        // 添加表头
        PdfPCell cell = new PdfPCell(new Phrase("项目", fontTitle));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("用量", fontTitle));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("单价", fontTitle));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("金额", fontTitle));
        table.addCell(cell);

        // 添加水费信息
        table.addCell(new PdfPCell(new Phrase("水费", font)));
        table.addCell(new PdfPCell(new Phrase(String.format("%.2f", bill.getWaterusage()), font)));
        table.addCell(new PdfPCell(new Phrase("1元/吨", font))); // 假设单价不在此账单中显示
        table.addCell(new PdfPCell(new Phrase(String.format("%.2f", bill.getWatercost()), font)));

        // 添加电费信息
        table.addCell(new PdfPCell(new Phrase("电费", font)));
        table.addCell(new PdfPCell(new Phrase(String.format("%.2f", bill.getElectricityusage()), font)));
        table.addCell(new PdfPCell(new Phrase("1元/度", font))); // 假设单价不在此账单中显示
        table.addCell(new PdfPCell(new Phrase(String.format("%.2f", bill.getElectricitycost()), font)));

        // 添加总金额
        cell = new PdfPCell(new Phrase("总金额", fontTitle));
        cell.setColspan(3);
        table.addCell(cell);
        table.addCell(new PdfPCell(new Phrase(String.format("%.2f", bill.getTotalamount()), font)));

        // 添加表格到文档
        document.add(table);

        // 添加剩余余额
        Paragraph remainingBalance = new Paragraph("账户余额: " + String.format("%.2f", bill.getRemainingbalance()), font);
        document.add(remainingBalance);

        document.close();
        return byteArrayOutputStream;
    }
}
